package com.itheima.service.impl;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = OrderService.class, protocol = "dubbo")
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderMapper orderMapper;

	@Override
	public Result order(Map map) throws Exception {
		//1.判断所选预约日期是否已经设置了预约日期
		String orderDate = (String) map.get("orderDate");
		//
		OrderSetting orderSetting = orderMapper.queryDateIsSet(DateUtils.parseString2Date(orderDate));

		if (orderSetting == null) {
			//2.预约的那天没有设置预约日期，返回
			return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
		}
		if (orderSetting.getReservations() >= orderSetting.getNumber()) {
			//3.所选的预约日期已经约满了，返回
			return new Result(false, MessageConstant.ORDER_FULL);
		}

		/*
			执行到这里，此时设置了预约日期 并且 预约人数没有满
		*/
		/*
		  4.为什么先判断用户是不是会员？不是会员先自动注册会员？
			因为判断是否重复预约有三个必要条件：1.用户id--->来源于会员表member，不是会员没有会员id，无法执行下一步判断！
												2.预约的日期
												3.套餐id
		 */
		//5.判断是否在重复预约
		String phoneNumber = (String) map.get("telephone");
		Member member = orderMapper.queryIsMember(phoneNumber);

		if (member != null) {
			//是会员
			Integer memberId = member.getId();
			Object setmealId = map.get("setmealId");
			String string = setmealId.toString();
			int i = Integer.parseInt(string);

			//判断用户是否再重复预约  是否同一天/同一个用户/同一个套餐
			Order order = new Order(memberId, DateUtils.parseString2Date(orderDate), i);
			//查询
			List<Order> orderList = orderMapper.findByOrder(order);
			if (orderList != null && orderList.size() > 0) {
				//已经完成了预约，不能重复预约
				return new Result(false, MessageConstant.HAS_ORDERED);
			}
		} else {
			//不是会员，自动注册
			member = new Member();
			member.setName((String) map.get("name"));
			member.setPhoneNumber(phoneNumber);
			member.setIdCard((String) map.get("idCard"));
			member.setSex((String) map.get("sex"));
			member.setRegTime(new Date());

			orderMapper.addMember(member);
		}
		//更新OrderSetting表
		int reservations = orderSetting.getReservations();
		reservations += 1;
		orderSetting.setReservations(reservations);
		orderMapper.updateOrderSetting(orderSetting);
		//执行预约操作
		//保存预约信息到预约表
		Order order = new Order(member.getId(),
				DateUtils.parseString2Date(orderDate),
				Order.ORDERTYPE_WEIXIN,
				Order.ORDERSTATUS_NO,
				Integer.parseInt((String) map.get("setmealId")));

		orderMapper.addOrder(order);
		return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());

	}

	@Override
	public Map findById(Integer id) throws Exception {
		Map map = orderMapper.findById(id);

		if (map!=null){
			Date orderDate = (Date) map.get("orderDate");
			String date = DateUtils.parseDate2String(orderDate);
			//覆盖之前的数据
			map.put("orderDate",date);
		}


		return map;
	}
}
